import APIZPZ from "../../../utils/apiZpz"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: <any>[],
    selectedId: 0,
    selected: false //是否可以选中地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data: any) {
    this.setData({
      selectedId: data.selectedId * 1,
      selected: data.selected
    });
  },
  addAddress() {
    wx.navigateTo({ url: '/pages/address/address-edit/address-edit' })
  },
  edit(e: any) {
    const data = e.currentTarget.dataset;
    console.log(data.address);
    wx.navigateTo({
      url: '/pages/address/address-edit/address-edit',
      success(res) {
        res.eventChannel.emit('addressData', { ...data.address })
      }
    })
  },
  delete(e: any) {
    const data = e.currentTarget.dataset;
    APIZPZ.deleteAddress(data.id).then(() => {
      this.getAddressList();
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 2000
      });
    })
  },
  getAddressList() {
    APIZPZ.getAddressList().then((res: any) => {
      console.log(res.data)
      res.data = res.data.map((item: any) => {
        item.addressPreview = item.exparea.split('@').join(' ') + ' ' + item.expdtl;
        return item;
      })
      this.setData({
        addressList: res.data
      })
    })
  },
  setAddress(e: any) {
    if (!this.data.selected) {
      return;
    }
    const data = e.currentTarget.dataset;
    wx.setStorageSync('addressId', data.address.id);
    wx.navigateBack({ delta: -1 });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddressList();
    wx.setStorageSync('addressInfo', undefined);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})